home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!not-for-mail
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Placement new = reconstruction?
- Date: 29 Feb 1996 15:45:41 GMT
- Organization: Clark Internet Services, Inc., Ellicott City, MD USA
- Message-ID: <4h4hn5$k8c@clarknet.clark.net>
- NNTP-Posting-Host: explorer.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- Do I understand correctly that placement new allows for reinitialization
- of an object in place, thereby giving two advantages:
-
- -- elimination of time spent repeatedly allocating and
- deallocating, and
-
- -- elimination of concern over proper destruction?
-
- In other words, if I have a class Bar that itself performs no allocation,
- and I need to perform an operation on a series of Bar objects, instead of
- allocating a new Bar object for each iteration, can I do the following? Is
- there any reason I shouldn't?
-
- Finally, is "placement delete" (i.e., a direct call to bar_->~Bar())
- required before each placement new, or before bar_ goes out of scope, or
- is it optional if Bar doesn't dynamically allocate anything and therefore
- doesn't need to DEallocate anything?
-
- class Bar
- {
- public:
- Bar(const char *stringArg = NULL);
- ~Bar() {}
- void doit();
- };
-
- Bar::Bar(const char *stringArg) { /* whatever */ }
- void Bar::doit() { / * whatever */ }
-
- class Foo
- {
- private:
- Bar bar_; // So bar_ will be destroyed automatically
- // when Foo is.
- public:
- Foo() {}
- ~Foo() {}
- void run(const char *stringArg = NULL);
- };
-
- void Foo::run(const char *stringArg)
- {
- new &bar_ Bar(stringArg);
- // Instead of deallocating and reallocating,
- // just create the new version of bar_ in place.
- bar_.doit();
- return;
- }
-
-